﻿Imports System.Data
Imports PadovadoAPI

Partial Class WebUserControls_ElencoLocali
    Inherits System.Web.UI.UserControl

    Public Property SqlGlobal As String
        Set(value As String)
            ViewState("SqlGlobal") = value
        End Set
        Get
            If String.IsNullOrEmpty(ViewState("SqlGlobal")) Then
                ViewState("SqlGlobal") = "SELECT  A.Denominazione as NomeLocale, isnull(O.imgLocaleString, '~/localimages/no-image.jpeg') as imgLocale, " & _
                                        " isnull(I.Strada,'') + ' ' + isnull(I.Civico,'') + ' ' + isnull(I.Comune,'') + ' ' + isnull(I.Presso,'') as Indirizzo, " & _
                                        " CASE isnull(O.intTipoLocale, 100) " & _
                                        "     WHEN 100 then 'http://www.padovado.it/locali-divertimenti/Generico/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50)) " & _
                                        "     WHEN 1 then 'http://www.padovado.it/locali-divertimenti/Ristorante/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50)) " & _
                                        "     WHEN 2 then 'http://www.padovado.it/locali-divertimenti/Ristobar/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50)) " & _
                                        "     WHEN 3 then 'http://www.padovado.it/locali-divertimenti/pub/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50)) " & _
                                        "     WHEN 4 then 'http://www.padovado.it/locali-divertimenti/discoteca/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50)) " & _
                                        "     WHEN 5 then 'http://www.padovado.it/locali-divertimenti/proloco/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 7 then 'http://www.padovado.it/locali-divertimenti/fiera/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 8 then 'http://www.padovado.it/locali-divertimenti/pizzeria/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 9 then 'http://www.padovado.it/locali-divertimenti/bar/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 10 then 'http://www.padovado.it/locali-divertimenti/EnteBenefico/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 11 then 'http://www.padovado.it/locali-divertimenti/museo/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 12 then 'http://www.padovado.it/locali-divertimenti/CentroCommerciale/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 13 then 'http://www.padovado.it/locali-divertimenti/Parrocchia/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 14 then 'http://www.padovado.it/locali-divertimenti/Cinema/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 15 then 'http://www.padovado.it/locali-divertimenti/Etnico/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 16 then 'http://www.padovado.it/locali-divertimenti/trattoria/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50))  " & _
                                        "     WHEN 6 then 'http://www.padovado.it/locali-divertimenti/organizzatoredieventi/SchedaLocale.aspx?PK_Locale=' + cast(O.PK_Organizzatore as varchar(50)) end as LinkInterno " & _
                                        " FROM Organizzatore O " & _
                                        " LEFT OUTER JOIN Indirizzo I ON I.PK_Indirizzo = O.FK_Indirizzo " & _
                                        " LEFT OUTER JOIN Anagrafica A ON A.PK_Anagrafica = O.FK_Anagrafica " & _
                                        " WHERE O.Deleted = 0 AND intTipoLocale in (1,2,3,4,8,9,14,15,16) AND 1 = 1" & _
                                        " ORDER BY ltrim(rtrim(A.Denominazione)) "
            End If
            Return ViewState("SqlGlobal")
        End Get
    End Property


    Protected Sub LinkButton_Click(sender As Object, e As System.EventArgs) Handles LinkButton_A.Click, LinkButton_B.Click, LinkButton_C.Click, LinkButton_D.Click, LinkButton_E.Click, LinkButton_F.Click, LinkButton_G.Click, _
     LinkButton_H.Click, LinkButton_I.Click, LinkButton_J.Click, LinkButton_K.Click, LinkButton_L.Click, LinkButton_M.Click, LinkButton_N.Click, LinkButton_O.Click, _
     LinkButton_P.Click, LinkButton_Q.Click, LinkButton_R.Click, LinkButton_S.Click, LinkButton_T.Click, LinkButton_U.Click, LinkButton_V.Click, LinkButton_X.Click, _
     LinkButton_W.Click, LinkButton_Z.Click, LinkButton_All.Click

        Dim filtro As String = sender.id.ToString.Split("_")(1).ToUpper
        If filtro = "ALL" Then
            CaricaLocali(SqlGlobal)
        Else
            Dim sqlLocal As String = SqlGlobal

            sqlLocal = sqlLocal.Replace("1 = 1", "rtrim(ltrim(A.Denominazione)) like '" & filtro & "%'")
            CaricaLocali(sqlLocal)
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Throw New Exception(SqlGlobal)
            CaricaLocali(SqlGlobal)

        End If
    End Sub

    Public Sub CaricaLocali(sql As String)
        Dim ds As New DataSet
        Dim dbConnector As New DBObject
        ds = dbConnector.FillDS(sql, Nothing, 1)

        If Not IsNothing(ds) AndAlso ds.Tables.Count > 0 Then

            If Not IsNothing(ds) AndAlso ds.Tables.Count > 0 Then
              
                For Each dr As DataRow In ds.Tables(0).Rows
                 
                    dr("LinkInterno") = dr("LinkInterno") & "&NomeLocale=" & dr("NomeLocale")
                Next
                ' Throw New Exception(ds.Tables.Count)
                RptLocali.DataSource = ds
                RptLocali.DataBind()
            End If


        End If
    End Sub
End Class
